package sliver.action.shop.ajax;

import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import sliver.entity.Order;
import sliver.service.shop.machine.MachineSettlementService;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Controller("shop.ajax.buyGoodsAction")
public class BuyGoodsAction extends ActionSupport{

	private static final long serialVersionUID = -1707406817609312766L;
	
	private Integer shopperId;
	private Long customId;
	private Long machineId;
	private Double totalPrice;
	private List<Double> itemBuyPrices = new ArrayList<Double>();
	private List<Double> itemBuyCounts = new ArrayList<Double>();
	private List<Integer> itemBuyGoodsIds = new ArrayList<Integer>();
	private List<String> itemBuyTypes = new ArrayList<String>();
	
	@Autowired
	private MachineSettlementService machineSettlementService;
	
	public BuyGoodsAction(){
		
	}

	public Integer getShopperId() {
		return shopperId;
	}

	public void setShopperId(Integer shopperId) {
		this.shopperId = shopperId;
	}

	public Long getCustomId() {
		return customId;
	}

	public void setCustomId(Long customId) {
		this.customId = customId;
	}

	public Long getMachineId() {
		return machineId;
	}

	public void setMachineId(Long machineId) {
		this.machineId = machineId;
	}
	
	public Double getTotalPrice() {
		return totalPrice;
	}

	public void setTotalPrice(Double totalPrice) {
		this.totalPrice = totalPrice;
	}

	public List<Double> getItemBuyPrices() {
		return itemBuyPrices;
	}

	public void setItemBuyPrices(List<Double> itemBuyPrices) {
		this.itemBuyPrices = itemBuyPrices;
	}

	public List<Double> getItemBuyCounts() {
		return itemBuyCounts;
	}

	public void setItemBuyCounts(List<Double> itemBuyCounts) {
		this.itemBuyCounts = itemBuyCounts;
	}

	public List<Integer> getItemBuyGoodsIds() {
		return itemBuyGoodsIds;
	}

	public void setItemBuyGoodsIds(List<Integer> itemBuyGoodsIds) {
		this.itemBuyGoodsIds = itemBuyGoodsIds;
	}

	public List<String> getItemBuyTypes() {
		return itemBuyTypes;
	}

	public void setItemBuyTypes(List<String> itemBuyTypes) {
		this.itemBuyTypes = itemBuyTypes;
	}

	@Override
	public String execute() throws Exception {
		
		Order order = machineSettlementService.settlement(
														shopperId, 
														customId, 
														machineId,
														itemBuyGoodsIds,
														itemBuyTypes,
														itemBuyCounts,
														itemBuyPrices,
														totalPrice
													);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=utf-8");
		
		Writer writer = response.getWriter();
		if(order != null)
			writer.write(JSON.toJSONString(order));
		else
			writer.write("{}");
		
		writer.flush();
		
		return null;
	}
}
